package com.zhixiang.core.web.action;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.zhixiang.core.entity.TreeObj;
import com.zhixiang.core.util.YearGridCrud;
import com.zhixiang.core.web.paging.PagingBean;


public class BaseAction extends ActionSupport
{
    private static final long serialVersionUID = 6768532902265842462L;

    /**
     * 是否成功
     */
    private boolean success = true;

    /**
     * 数据的条数
     */
    private Integer totalCounts = 0;

    /**
     * 数据
     */
    private Object result;

    /**
     * 信息
     */
    private String msg;

    
    public String dir;

    public String sort;

    public Integer limit = Integer.valueOf(25);

    public Integer start = Integer.valueOf(0);

    public boolean isSuccess()
    {
        return success;
    }

    public void setSuccess(boolean success)
    {
        this.success = success;
    }

    public Integer getTotalCounts()
    {
        return totalCounts;
    }

    public void setTotalCounts(Integer totalCounts)
    {
        this.totalCounts = totalCounts;
    }

    public Object getResult()
    {
        return result;
    }

    public void setResult(Object result)
    {
        this.result = result;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg(String msg)
    {
        this.msg = msg;
    }
    
    public String getDir()
    {
        return dir;
    }

    public void setDir(String dir)
    {
        this.dir = dir;
    }

    public String getSort()
    {
        return sort;
    }

    public void setSort(String sort)
    {
        this.sort = sort;
    }

    public Integer getLimit()
    {
        return limit;
    }

    public void setLimit(Integer limit)
    {
        this.limit = limit;
    }

    public Integer getStart()
    {
        return start;
    }

    public void setStart(Integer start)
    {
        this.start = start;
    }
    

    /////////////////////////////////////////

    protected HttpServletRequest getRequest()
    {
        return ServletActionContext.getRequest();
    }

    protected HttpServletResponse getResponse()
    {
        return ServletActionContext.getResponse();
    }

    protected HttpSession getSession()
    {
        return getRequest().getSession();
    }

    protected PagingBean getInitPagingBean()
    {
        PagingBean localPagingBean = new PagingBean(this.start.intValue(), this.limit.intValue());
        return localPagingBean;
    }

    public String execute()
        throws Exception
    {
        HttpServletRequest localHttpServletRequest = getRequest();
        String str1 = localHttpServletRequest.getRequestURI();
        String str2 = str1.substring(localHttpServletRequest.getContextPath().length());
        str2 = str2.replace(".do", ".jsp");
        str2 = "/pages" + str2;
        setResult(str2);
        return SUCCESS;
    }
    
    public void outString(String str) {
        try {
            PrintWriter out = getResponse().getWriter();
            out.write(str);
            out.flush();
            out.close();
            out = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void outJsonString(String str) {
        getResponse().setContentType("text/javascript;charset=UTF-8");
        outString(str);
    }
    public void outXMLString(String str) {
    	getResponse().setContentType("text/xml;charset=UTF-8");
    	outString(str);
    }

    public void outHtmlString(String str) {
        getResponse().setContentType("text/html;charset=UTF-8");
        outString(str);
    }
    
    //实现年份数的方法
    public String treeLoad(){
        List<TreeObj> treeList=YearGridCrud.treeLoad();
        if(treeList!=null){
            outJsonString(new Gson().toJson(treeList));
        }       
        return SUCCESS;
    }

}
